
local Obj = require("core.Obj")
local Timer = require("core.Timer")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local DropLogic = require("monster.DropLogic")
local Grid = require("bag.Grid")
local ItemDefine = require("bag.ItemDefine")
local Equip = require("bag.Equip")
local BagLogic = require("bag.BagLogic")


life_time_start = 120000							-- 生存时间 30s
life_time_end = 180000							-- 生存时间 40s

life_time_not_good_equip_start = 90000			--　quality < 4 生存时间　90s
life_time_not_good_equip_end = 100000			--　quality < 4 生存时间　100s

life_time_good_equip_start = 300000				--　quality >=4 生存时间　5min
life_time_good_equip_end = 360000				--　quality >=4 生存时间　6min

list = list or {} -- 物品列表

function checkExpire()
    local leftTime
    local isContinue
    for k, v in pairs(list) do
        isContinue = false
        leftTime = v.life_time - Timer.now + v.create_time
        if v.owner_uuid and not v.del and leftTime < 4000 then	--为了用户体验，提前让用户删掉
            v.del = 1
            del(v)
            isContinue = true
        end

        if leftTime <= 0 and not isContinue then
            if not v.del and v.owner_uuid then
                assert()
            end
            v.die = 1
            destroy(v)
        end

    end
end

function hasGoodEquip(human)
    for k, v in pairs(list) do
        if v.owner_uuid == human._id and ItemDefine.isEquip(v.grid.id) and v.grid.data.equip.quality >= 5 then
            return true
        end
    end
end

function create(item_id, owner_uuid, bind, cnt, src_x, src_y, qualityOut, fromMonsterType, sendArea, dropType)
    if not item_id then
        assert()
    end
    local item = {}
    Obj.create(item, Obj.TYPE_ITEM)
    item.item_id = item_id
    item.owner_uuid = owner_uuid
    item.bind = bind and 1 or 0
    item.cnt = cnt
    item.create_time = Timer.now
    item.life_time = math.random(life_time_start, life_time_end)
    item.src_x = src_x
    item.src_y = src_y
    item.grid = {}
    Grid.create(item.grid, item_id, cnt, bind, qualityOut)
    item.die = nil	-- 移出视移与拾取情况标识
    item.del = nil	-- 提前移除标志
    item.dropFromHuman = nil -- 人物丢弃
    item.dropFromMonsterType = fromMonsterType 
    item.drop_type = dropType or 0
    item.sendArea = sendArea
    if ItemDefine.isEquip(item.grid.id) then -- 紫装
        if item.grid.data.equip.quality >= 4 then
            item.life_time = math.random(life_time_good_equip_start, life_time_good_equip_end)
        else
            item.life_time = math.random(life_time_not_good_equip_start, life_time_not_good_equip_end)
        end
    end

    if list[item.id] then
        assert()
    end
    list[item.id] = item

    return item
end

function enterScene(item, scene_id, x, y)
    if not item.owner_uuid or item.sendArea then
        Obj.enterScene(item, scene_id, x, y)
    else
        local human = ObjHuman.onlineUuid[item.owner_uuid]
        if not human then
            return
        end
        item.x = x
        item.y = y
        item.src_scene_id = scene_id
        local msgRet = getObjAdd(item)
        Msg.send(msgRet, human.fd)
    end
end

function getObjAdd(item)
    local msgRet = Msg.gc.GC_ADD_ITEM
    msgRet.owner_uuid = item.owner_uuid or ""
    msgRet.obj_id = item.id
    msgRet.staticID = item.item_id
    msgRet.count = item.cnt
    msgRet.subType1 = ItemDefine.getValue(item.item_id, "subType1")
    local name = ItemDefine.getValue(item.item_id, "name")
    msgRet.name = name
    if ItemDefine.is_spobj[item.item_id] then
        msgRet.useBagCap = 0
    else
        msgRet.useBagCap = 1
    end
    msgRet.drop_icon = ItemDefine.getValue(item.item_id, "dropIcon")
    msgRet.drop_effect = ItemDefine.getValue(item.item_id, "dropEffect") or 0
    msgRet.icon = ItemDefine.getValue(item.item_id, "icon")
    if ItemDefine.spobj.yinliang[item.item_id] and item.cnt > 2000 then	-- 铜币 > 2000时，发特殊icon
        msgRet.icon = 52
    end
    msgRet.color = ItemDefine.getValue(item.item_id, "color")
    if ItemDefine.isEquip(item.item_id) then
        msgRet.color = Equip.getColor(item.grid.data.equip.quality)
    end
    msgRet.x = item.x
    msgRet.y = item.y
    if Timer.now - item.create_time <= 10 then
        msgRet.src_x = item.src_x
        msgRet.src_y = item.src_y
    else
        msgRet.src_x = -1
        msgRet.src_y = -1
    end
    msgRet.drop_type = item.drop_type
    return msgRet
end

function del(item)
    local human = ObjHuman.onlineUuid[item.owner_uuid]
    if item.owner_uuid and item.del and human and human.scene_id == item.src_scene_id then
        local msgRet = Msg.gc.GC_DEL_OBJ
        msgRet.obj_id = item.id
        msgRet.die = item.die and item.die or 0
        Msg.send(msgRet, human.fd)
    end
end

function destroy(item)
    if not item.del then
        item.del = 1
        del(item)
    end

    if list[item.id] ~= item then
        assert()
    end

    list[item.id] = nil
    Obj.destroy(item)
end

function onHumanEnterScene(human)
    addHumanDropItem(human)
end

function addHumanDropItem(human)
    for k, v in pairs(list) do
        if v.owner_uuid == human._id and human.scene_id == v.src_scene_id and not v.del then
            local msgRet = getObjAdd(v)
            Msg.send(msgRet, human.fd)
        end
    end
end

function objItemBloodAi()
    local now = Timer.now
    for _, v in pairs(list) do
        local scene = Obj.scenes_fds[v.scene_id] or Obj.scenes_fds[v.src_scene_id]
        local time = 1000
        if (ItemDefine.is_spobj[v.grid.id] or (v.grid.id == 10029)) and scene and now - v.create_time > time and not v.del then
            for _, vv in pairs(scene) do
                if 0 < vv.db.hp and (v.owner_uuid and v.owner_uuid == vv._id) then
                    local dx = v.x - vv.x
                    local dy = v.y - vv.y
                    local r = 1000
                    if -r < dx and dx < r and -r < dy and dy < r then
                        if v.grid.id ~=10029 or BagLogic.checkCanAddItem(vv, v.grid.id, v.grid.bind, v.grid.cnt, v.grid.lifeTime) then
                          DropLogic.pickUpItem(vv, v.id)
                        end
                        break
                    end
                end
            end
        end
    end
end


function clearOwnerItemByScene(sceneID)
    local needDestroy = {}
    for _, objItem in pairs(list) do
        if objItem.src_scene_id == sceneID and not objItem.del then
            needDestroy[#needDestroy + 1] = objItem
        end
    end

    for i = 1, #needDestroy do
        destroy(needDestroy[i])
    end
end

function clearOwnerItemBySceneAndItemID(sceneID, itemID)
    local needDestroy = {}
    for _, objItem in pairs(list) do
        if objItem.src_scene_id == sceneID and objItem.item_id==itemID and not objItem.del then
            needDestroy[#needDestroy + 1] = objItem
        end
    end

    for i = 1, #needDestroy do
        destroy(needDestroy[i])
    end
end
